using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Presentation.Contracts;
using Presentation;
using Globals;

namespace ProjektZespolowy.Mail
{
    public partial class Inbox : System.Web.UI.Page,IInboxView
    {
        #region Members

        private InboxPresenter presenter;

        #endregion Members

        #region Override Methods

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter = new InboxPresenter(this);
        }
   

        #endregion Override Methods

        protected void Page_Load(object sender, EventArgs e)
        {
            presenter.GetAllReceivedMsg();
            presenter.SetCaption();
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.DataSource = ViewState["tabela1"];
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            HyperLink tmp = (HyperLink)GridView1.Rows[e.RowIndex].Cells[1].Controls[1];
            string a = tmp.NavigateUrl;
            string[] tab =a.Split('=');
           int msg_id = int.Parse(tab[1]);

           presenter.DelMessage(msg_id);

           Response.Redirect("/Mail/Inbox.aspx");


        }

       
    protected void BoldNewMsg(object sender,  EventArgs e)
    {
        HyperLink hpl = sender as HyperLink;
     
        string a = hpl.NavigateUrl;
        string[] tab = a.Split('=');
        int msg_id = int.Parse(tab[1]);

        string s=presenter.GetStatOfMessage(msg_id);

        if (s.CompareTo("n") == 0)
        {
            hpl.Font.Bold = true;
            hpl.ForeColor = System.Drawing.Color.Red;
        }
        
    }

      

        #region IInboxView Members

        public void ShowMessages(DataTable dt)
        {
                ViewState["tabela1"] = dt;
                GridView1.DataSource = ViewState["tabela1"];
                GridView1.DataBind();
        }

        public string SessionID()
        {

          return Session[ContentNamesGlobals.SessionWithUserId].ToString();

        }

        public void SetHeader()
        {
            lHeader1.Text = "Odebrane wiadomosci (" + presenter.GetNumberOfReceivedMessages().ToString() + ")";
        }

        #endregion    

    }
}
